package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePage;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl extends ServiceImpl<StatementMapper, Statement> implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private IStatementItemService statementItemService;

    @Override
    public TablePage<Statement> query(StatementQuery qo) {
        Page<Statement> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Statement> wrapper = Wrappers.<Statement>query();
        wrapper.like(StringUtils.isNotEmpty(qo.getCustomerName()),"customer_name",qo.getCustomerName())
                .like(StringUtils.isNotEmpty(qo.getCustomerPhone()),"customer_phone",qo.getCustomerPhone())
                .like(StringUtils.isNotEmpty(qo.getLicensePlate()),"license_plate",qo.getLicensePlate());
        statementMapper.selectPage(page, wrapper);
        return new TablePage<Statement>(page);
    }


    @Override
    public void add(Statement statement) {
        //  确保新增的结算单状态为消费中
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectById(id);
    }


    @Override
    public void update(Statement statement) {
        // 只有消费中的状态才能编辑
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) { // 不是消费中的状态
            throw new BusinessException("只有消费中才能编辑");
        }
        // 在页面显示的编辑要素,才在SQL中去修改,其他的就不用再SQL中提供字段去修改了
        UpdateWrapper<Statement> wrapper = Wrappers.<Statement>update();
        wrapper.eq("id",statement.getId());
        wrapper.set("customer_name",statement.getCustomerName())
                .set("customer_phone",statement.getCustomerPhone())
                .set("actual_arrival_time",statement.getActualArrivalTime())
                .set("license_plate",statement.getLicensePlate())
                .set("car_series",statement.getCarSeries())
                .set("service_type",statement.getServiceType())
                .set("info",statement.getInfo());
        statementMapper.update(null,wrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            // 只有消费中才能删除
            Statement statement = this.get(dictId);
            if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
                throw new BusinessException("只有消费中才能删除结算单");
            }
            // 删除前要先删除掉结算单明细
            statementItemService.deleteByStatementId(dictId);
            statementMapper.deleteById(dictId);
        }
    }

    @Override
    public List<Statement> selectForList() {
        return statementMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public Statement selectByAppointmentId(Long appointmentId) {
        QueryWrapper<Statement> wrapper = Wrappers.<Statement>query();
        wrapper.eq("appointment_id",appointmentId);
        return statementMapper.selectOne(wrapper);
    }

    @Override
    public void updateConsumer(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountAmount) {
        UpdateWrapper<Statement> wrapper = Wrappers.<Statement>update();
        wrapper.eq("id",statementId);
        wrapper.set("total_amount",totalAmount)
                .set("total_quantity",totalQuantity)
                .set("discount_amount",discountAmount);
        statementMapper.update(null,wrapper);
    }

    @Override
    public void updatePaid(Long statementId, Long userId, Integer statusPaid) {
        UpdateWrapper<Statement> wrapper = Wrappers.<Statement>update();
        wrapper.eq("id",statementId);
        wrapper.set("status",statusPaid)
                .set("payee_id",userId)
                .set("pay_time",new Date());
        statementMapper.update(null,wrapper);
    }
}
